package cn.itcast.contorller.dept;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.Dept;
import cn.itcast.service.dept.DeptService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/system/dept")
public class DeptController extends BaseContorller {
    @Autowired
    private DeptService deptService;
    @RequestMapping(value = "/list",name = "pageHelper插件分页查询部门")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        /*因为共享库共享表所以需要每个公司来查询每个公司的部门根据company_id，就是标识字段*/
       PageInfo pageInfo= deptService.findAll(pageNum,pageSize,company_id);
       request.setAttribute("pb",pageInfo);
        System.out.println(pageInfo);
       return "system/dept/dept-list";
    }
    @RequestMapping(value = "/toAdd",name = "到新建部门页面")
    public String toAdd(){
      List<Dept> list =deptService.findAllDept(company_id);
        System.out.println(list);
      request.setAttribute("deptList",list);
      return "system/dept/dept-add";
    }
    @RequestMapping(value = "/edit.do",name = "部门保存/部门更新")
    public String edit(Dept dept){
        //指定当前部门是属于哪个公司的
        dept.setCompanyId(company_id);
        dept.setCompanyName(company_name);
        if (UtilFuns.isEmpty(dept.getId())){
            /*新增*/
            dept.setId(UUID.randomUUID().toString());
            deptService.save(dept);
        }else {
            /*如果不设置企业的id就会覆盖所以在上边写*/
            deptService.update(dept);
        }
        return  "redirect:/system/dept/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "到修改页面")
    public String toUpdate( String id){
        Dept dept=  deptService.toUpdate(id);
        request.setAttribute("dept",dept);
        List<Dept> allDept = deptService.findAllDept(company_id);
        request.setAttribute("deptList",allDept);
        return "system/dept/dept-update";
    }
    @RequestMapping(value = "/delete",name = "删除部门表")
    public String delete(String id){
        deptService.delete(id);
        return  "redirect:/system/dept/list.do";
    }



}
